//人员照片上传
var img=null; 
function previewImg(obj){	
  	if(img)img.removeNode(true);   
  	img=document.createElement("img");   
  	img.style.position="absolute";   
  	img.style.visibility="hidden"; 
  	img.style.left="0px";
  	img.style.top="0px";
  	img.attachEvent("onreadystatechange",trueImg);
  	img.attachEvent("onerror",falseImg);  	
  	document.body.insertAdjacentElement("beforeend",img);   
  	img.src=obj.value;
  	
} 
//上传失败  
function  falseImg(){ 
	var iPhoto=document.getElementById("photo");
	iPhoto.src="/SCMDB/images/noEmpPhoto.gif";
	iPhoto.style.width="80px";
	iPhoto.style.height="100px";
  	var uploadObj=document.getElementById("employeeAction_empPhoto");
  	var uploadTdObj=document.getElementById("uploadTd");
	var newUpLoad=uploadTdObj.innerHTML;
   	var strTxt="请上传正确的头像文件！" 
	//alertMsg("",strTxt,0);
	//清空之前选择的文件

	uploadTdObj.removeChild(uploadObj);
	uploadTdObj.innerHTML=newUpLoad;
	//清空长宽值

	return false;  
}
//上传成功   
function trueImg(){ 
	var iPhoto=document.getElementById("photo");
  	if(img.readyState!="complete")return false;   
   	iPhoto.src=document.getElementById("employeeAction_empPhoto").value;
   	//当图片长宽都小于100时，显示为原来大小

   	if(img.offsetWidth<100 && img.offsetHeight<100){
   		iPhoto.style.width=img.offsetWidth+"px";
   		iPhoto.style.height=img.offsetHeight+"px";
   	}
   	//当图片长或者宽大于100PX时，缩小显示
   	else if(img.offsetWidth>img.offsetHeight){
   		iPhoto.style.width="100px";
   		var rate=img.offsetWidth/100;
   		iPhoto.style.height=img.offsetHeight/rate+"px";
   	}else{
   		iPhoto.style.height="100px";
   		var rate=img.offsetHeight/100;
   		iPhoto.style.width=img.offsetWidth/rate+"px";
   	}
   img.style.display="none";
}  

//判断是否为特种人员，如果是特种人员，则需要添加该人员需要经过的区域及时间
function addDisplay(){
	//alert(0);
	if(!document.getElementById("employeeAction_entity_empType")) return false;
	var typeEmp=document.getElementById("employeeAction_entity_empType");
	if(!document.getElementById("hidTr1")) return false;
	//alert(typeEmp);
	if(typeEmp.value=="0"){
		var hidTr1=document.getElementById("hidTr1");
		hidTr1.style.display="none";
	}
	if(typeEmp.value=="1"){
		var hidTr1=document.getElementById("hidTr1");
		hidTr1.style.display="";
	}
}
window.onload=function(){
	addDisplay();
	initReID();
}
//add ct
function addObj(id1,id2,flagNum){
	var divObj=document.getElementById(id1);
	var pObj=divObj.getElementsByTagName("p");
	var pObjOth=document.createElement("p");
	pObjOth.style.padding="0px";
	pObjOth.style.margin="0px";
	pObjOth.id=id2+pObj.length;
	var nowTime=getNowDate();
	var area = document.getElementById("employeeAction_areaList");
	var selectObjText = area.options[area.selectedIndex].text;
	var selectObjValue = area.options[area.selectedIndex].value;
	if(id2=="ctP"){
		pObjOth.innerHTML="<input type=\"text\" value=\""+selectObjText+"\" readOnly=\"true\"/><input type=\"hidden\" name=\"entity.area\" value=\""+selectObjValue+"\"/>时间范围：<input type=\"text\" name=\"entity.startTime\" title=\"开始时间\" style=\"width:90px\" class=\"Wdate\" id=\"d414\" onfocus=\"WdatePicker({dateFmt:'H:mm:ss'})\"/>~<input type=\"text\" name=\"entity.endTime\" title=\"结束时间\" style=\"width:90px\" class=\"Wdate\" id=\"d414\" onfocus=\"WdatePicker({dateFmt:'H:mm:ss'})\"/><img src='/SCMDB/images/del.gif' class='delImg' onclick='delLicense(\"ctP"+pObj.length+"\",\"ctDivP\")' title='删除该项' />"
		//alert(pObjOth.innerHTML);
	}
	
	divObj.appendChild(pObjOth);	
}
function getNowDate(){
	var date = new Date(); //日期对象
	var nowTime = "";
	nowTime = date.getFullYear()+"-"; //读英文就行了
	nowTime = nowTime + (date.getMonth()+1)+"-"; //取月的时候取的是当前月-1如果想取当前月+1就可以了
	nowTime = nowTime + date.getDate()+" ";
	nowTime = nowTime + date.getHours()+":";
	nowTime = nowTime + date.getMinutes()+":";
	nowTime = nowTime + date.getSeconds()+"";
	return nowTime;
}


function delLicense(pId,divId){	
	if(!document.getElementById(divId)) return false;
	var divObj=document.getElementById(divId);	
	if(!document.getElementById(pId)) return false;
	var pObj=document.getElementById(pId);	
	divObj.removeChild(pObj);	
}


var arrArea = "";// 存储读头信息
function getParent() {
	var area = document.getElementById("areaType");
	var readDiv = document.getElementById("limitAreas");
	if(area.selectedIndex==-1){
		alert("区域为空！");
		return false;
	}
	var areaID = area.options[area.selectedIndex].value;
	var areaName = area.options[area.selectedIndex].text;
	if (checkArea(areaID, areaName) && areaID!=0) {
		var span = document.createElement("p");
		span.setAttribute("id", "spanLimit" + areaID);
		span.style.padding="0px";
		span.style.margin="0px";
		readDiv.appendChild(span);
		var spanID = "spanLimit" + areaID;
		span.innerHTML = "<input type='text' value='"
				+ areaName
				+ "' class='selInput' readonly=''/><img src='/SCMDB/images/del.gif' class='delImg' onclick='delParent(\""
				+ spanID + "\",\"" + areaID + "\")' title='删除该项' />";
		if (arrArea == "") {
			arrArea = areaID;
		} else {
			arrArea = arrArea + "," + areaID;
		}
		document.getElementById("limitAreaID").value = arrArea;
	}
}
function initReID() {
	var readDiv = document.getElementById("limitAreas");
	if(readDiv==null){
		return false;
	}
	var areas = document.getElementById("limitAreaID").value;
	if (areas){
		var arr = areas.split(",");
		var area = document.getElementById("areaType");
		var ops = area.options;
		for ( var read = 0; read < arr.length; read++) {
			for ( var i = 0; i < ops.length; i++) {
				if (arr[read] == ops[i].value) {
					if (checkArea(ops[i].value, ops[i].text)) {
						var span = document.createElement("span");
						span.setAttribute("id", "spanLimit" + ops[i].value);
						readDiv.appendChild(span);
						span.className = "addSp";
						var spanID = "spanLimit" + ops[i].value;
						span.innerHTML = "<input type='text' value='"
								+ ops[i].text
								+ "' class='selInput' readonly=''/><img src='/SCMDB/images/del.gif' class='delImg' onclick='delParent(\""
								+ spanID + "\",\"" + ops[i].value + "\")' title='删除该项' />";
						if (arrArea == "") {
							arrArea = ops[i].value;
						} else {
							arrArea = arrArea + "," + ops[i].value;
						}
						document.getElementById("limitAreaID").value = arrArea;
					}
				}
			}
		}
	}	
}
function checkArea(areaID, areaName) {
	var areas = arrArea.split(",");
	for ( var i = 0; i < areas.length; i += 1) {
		if (areaID == areas[i]) {
			popupCount = 1;
			Super.Popup (2, "警告", "", areaName + "已经添加！");
			return false;
		}
	}
	return true;
}
function delArea(areaID) {
	var areas = arrArea.split(",");
	var lastArea = "";
	for ( var i = 0; i < areas.length; i += 1) {
		if (areaID != areas[i]) {
			if (lastArea == "") {
				lastArea = areas[i];
			} else {
				lastArea = lastArea + "," + areas[i];
			}
		}
	}
	arrArea = lastArea;
	document.getElementById("limitAreaID").value = arrArea;
}
function delParent(spanID, areaID) {
	document.getElementById("limitAreas").removeChild(document.getElementById(spanID));
	delArea(areaID);
}
/**
 * 在卡号输入框内容变化的时候，动态的从数据库获取匹配的卡号列表
 * @return
 */
function getTags(obj){
	var parameter = {
			"tagNo" :obj.value
	}
	var url="/SCMDB/empAction!getTags.action";
	jQuery.post(url, parameter, setTags);
}
function setTags(result){
	var data = eval("("+result+")");
	var tag = document.getElementById("employeeAction_entity_empLabelID");	
	var tagsDiv = document.getElementById("tagsDiv");
	if (tagsDiv){
		tagsDiv.innerHTML = "";
		var str = "";
		for (var j = 0; j < data.length; j++) {
			str = str + "<li style='list-style-type:none;text-align:left' onmouseover='mouseIn(this)' onmouseout='mouseOut(this)' onclick='select(this)'>" + data[j].tagno + "</li>";
		}
		str = str + "";
		if (data.length > 8){
			tagsDiv.style.height = 8 * 15 + "px";
		} else {
			tagsDiv.style.height = data.length * 15 + "px";
		}
		tagsDiv.style.width = "120px";
		tagsDiv.style.left=pageX(tag)+ "px";
		tagsDiv.style.top=pageY(tag) + 19 + "px";
		tagsDiv.style.zIndex = "999";
		tagsDiv.style.position = "absolute";
		tagsDiv.style.background="#fff";
		tagsDiv.style.border = "1px solid #ccc";
		tagsDiv.style.overflow = "auto";
		tagsDiv.style.position = "absolute";
		tagsDiv.style.display = "";
		tagsDiv.innerHTML = str;
	}
}
function mouseOut(obj){	
	obj.style.background="#fff";
}
function mouseIn(obj){	
	obj.style.background="#4876FF";	
}
function select(obj){
	document.getElementById("employeeAction_entity_empLabelID").value = obj.innerHTML;
	document.getElementById("tagsDiv").style.display="none";	
}
document.onclick=function(){	
	var tagsDiv = document.getElementById("tagsDiv");
	tagsDiv.style.display="none";
}
function pageX(elem) {
	return elem.offsetParent ? elem.offsetLeft + pageX(elem.offsetParent) : elem.offsetLeft;
}
function pageY(elem) {
    return elem.offsetParent ? elem.offsetTop + pageY(elem.offsetParent) : elem.offsetTop;
}